package main

import (
	"flag"
    "bufio"
    "io"
    "os"
    "fmt"
)



func buf_read(filePth string, buf []byte, bufbufSize int) int {
    f, err := os.Open(filePth)
    if err != nil {
    	fmt.Printf("open file failed\n")
        return -1
    }
    defer f.Close()
    
	bfRd := bufio.NewReader(f)

	n, err := bfRd.Read(buf)
	if err != nil && err != io.EOF { //遇到任何错误立即返回，并忽略 EOF 错误信息
		fmt.Printf("read file failed\n")
	    return -2
	}
    return n
}

/*
slice1 := make([]type, len)
定义切片，是数组的抽象，在go里面用得比数组多

byte = uint8

range 的用法

*/

func main() {
	var fileName string
	flag.StringVar(&fileName, "file", "file.mp3", "file name")
	flag.Parse()
	fmt.Println("file name:", fileName)

	var buflen int = 1024*1024
	buf := make([]byte, buflen)
	fmt.Printf("buf len=%d cap=%d\n", len(buf), cap(buf))
	
    buflen= buf_read(fileName, buf, buflen)

    fmt.Printf("read file len=%d\n", buflen)

    if buflen <= 64 {
		for _, n := range buf[:buflen] {
		    fmt.Printf("%02x ", n )
		}
		fmt.Printf("\n")
    }
}
